# -*- coding:utf-8 -*-
# File: lab_cputemp.py
#向onenet平台已经创建的数据流发送数据点
import urllib.request
import json
import time
import datetime

APIKEY = 'xWOBphPlFSp9NJYKKWjhzzelgBo='  #改成你的APIKEY,需要在设备里创建

def get_temp():
        # 打开文件
        file = open("/sys/class/thermal/thermal_zone0/temp")
        # 读取结果，并转换为浮点数
        temp = float(file.read()) / 1000
        # 关闭文件
        file.close()
        # 向控制台打印结果
        print ("CPU的温度值为: %.3f" %temp)
        # 返回温度值
        return temp
        
        
def http_post():
    temperature = get_temp() #获取CPU温度并上传
    CurTime = datetime.datetime.now()
    url='http://api.heclouds.com/devices/921282510/datapoints'
    values={'datastreams':[{"id":"temp_cpu","datapoints":[{"at":CurTime.isoformat(),"value":temperature}]}]}

    print ("当前的ISO时间为: %s" %CurTime.isoformat())
    print ("上传的温度值为: %.3f" %temperature)

    jdata = json.dumps(values)                  # 对数据进行JSON格式化编码，生成的是字符串
    #打印json内容
    print (jdata)
    data = bytes(jdata, 'utf-8')
    request = urllib.request.Request(url, data)
    request.add_header('api-key', APIKEY)
    request.get_method = lambda:'POST'          # 设置HTTP的访问方式
    request = urllib.request.urlopen(request)
    return request.read()
    

while True:
        time.sleep(5)
        resp = http_post()
        print ("OneNET请求结果:\n %s" %resp)
        time.sleep(5)
